# -*- coding: utf-8 -*-
# @Time : 2021/10/24 21:12 
# @Author : TanDaBao
# @File : lishipin.py

from lxml import etree
import requests,os,re

# 创建存储图片目录
dirName = 'Temporary'
if not os.path.exists(dirName):
    os.mkdir(dirName)

# 用于存放视频标题与下载url
urls = []

proxies = {"http": None, "https": None}
headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36'
}

url = 'https://www.pearvideo.com/category_5'
response = requests.get(url=url, proxies=proxies, headers=headers)
response.encoding = 'utf-8'
html = response.text

tree = etree.HTML(html)
location = tree.xpath('//*[@id="listvideoListUl"]/li/div/a')
for addr in location:
    # 获取详情页url、标题
    addr_path = 'https://www.pearvideo.com/' + addr.xpath('./@href')[0]
    title = addr.xpath('./div[2]/text()')[0] + '.mp4'
    # print(addr_path, title)

    # 详情页信息
    response = requests.get(url=addr_path, proxies=proxies, headers=headers)
    response.encoding = 'utf-8'
    son_html = response.text
    # 获取contID用于拼接真实url
    contId = re.search('contId\s=\s"(.*?)"', son_html).group(1)
    cont_id = 'cont-' +contId

    # 携带Referer访问也就是详情页连接的url
    headers = {
    'Referer': addr_path,
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36'
    }

    # 会话保持
    session = requests.Session()
    json_url = 'https://www.pearvideo.com/videoStatus.jsp?contId=' + contId  # 其中contId为视频的编号id
    response = session.get(url=json_url, proxies=proxies, headers=headers)
    dic_obj = response.json()

    # 被伪装的下载地址
    down_url = dic_obj['videoInfo']['videos']['srcUrl']
    # print(down_url)

    # 替换出真实的下载地址
    string = re.search(r'/third/\d+/(\d+)-.*?', down_url).group(1)
    real_url = down_url.replace(string, cont_id)

    # 存储视频标题与真实下载url
    dic = {
        'title' : title,
        'url' : real_url
    }

    urls.append(dic)

# 下载视频
for item in urls:
    title = item['title']
    url = item['url']
    print(title,'下载中..')
    video_path = dirName + '/' + title
    video = requests.get(url=url, headers=headers, proxies=proxies)
    if video.status_code == 200:
        with open(video_path, 'wb') as f:
            f.write(video.content)
        print(title, '下载完成')
    else:
        print('下载url异常')
